local muzhi = fk.CreateSkill {
  name = "kl__muzhi",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["kl__muzhi"] = "暮志",
  [":kl__muzhi"] = "锁定技，当“鬼”角色第一次对你造成伤害时，此伤害+1；当你第一次对“鬼”角色造成伤害时，其失去1点体力。"..
  "当“鬼”角色死亡时，你获得其一个技能描述含有“伤害”的技能。",

  ["#kl__muzhi-invoke"] = "暮志：请选择 %dest 的一个技能描述含有“伤害”的技能获得",

  ["$kl__muzhi1"] = "身拄长刀，兀立斜阳，何妨以血补天。",
}

muzhi:addEffect(fk.DamageCaused, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(muzhi.name) and data.from then
      local frommark = player:getTableMark("klmo__muzhi_from_times"..player.id)
      local tomark = player:getTableMark("klmo__muzhi_to_times"..player.id)
      return (data.from == player and data.to:getMark("@@klmo__huangzhong_gui") > 0 and not table.contains(tomark, tostring(data.to.id)))
      or (data.to == player and data.from:getMark("@@klmo__huangzhong_gui") > 0 and not table.contains(frommark, tostring(data.from.id)))
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(muzhi.name)
    if data.from == player and data.to:getMark("@@klmo__huangzhong_gui") > 0 then
      room:notifySkillInvoked(player, muzhi.name, "offensive")
      room:addTableMarkIfNeed(player, "klmo__muzhi_to_times"..player.id, tostring(data.to.id))
      room:loseHp(data.to, 1, muzhi.name)
    end
    if data.to == player and data.from:getMark("@@klmo__huangzhong_gui") > 0 then
      room:notifySkillInvoked(player, muzhi.name, "negative")
      room:addTableMarkIfNeed(player, "klmo__muzhi_from_times"..player.id, tostring(data.from.id))
      data:changeDamage(1)
    end
  end,
})

muzhi:addEffect(fk.Death, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(muzhi.name) and target:getMark("@@klmo__huangzhong_gui") > 0  then
      local choices = {}
      for _, skill_name in ipairs(target:getSkillNameList()) do
        local skill = Fk.skills[skill_name]
        local skill_desc = Fk:translate(":"..skill_name, "zh_CN")
        if not player:hasSkill(skill, true) and string.find(skill_desc, "伤害") then
          table.insertIfNeed(choices, skill_name)
        end
      end
      if #choices > 0 then
        event:setCostData(self, {extra_data = choices})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local skills = event:getCostData(self).extra_data
    local result = room:askToCustomDialog(player, {
      skill_name = muzhi.name,
      qml_path = "packages/utility/qml/ChooseSkillBox.qml",
      extra_data = {
        skills, 0, 1, "#kl__muzhi-invoke::"..target.id,
      },
    })
    if result then
      if result[1] then
        event:setCostData(self, { choice = result[1]})
      else
        event:setCostData(self, { choice = table.random(skills)})
      end
    else
      event:setCostData(self, { choice = table.random(skills)})
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local choice = event:getCostData(self).choice
    if choice then
      player.room:handleAddLoseSkills(player, choice)
    end
  end,
})


return muzhi
